package com.yanqu.road.server.gameplayer.module.activity.cat.action;

import com.yanqu.road.entity.activity.cat.EleAttr;
import com.yanqu.road.entity.activity.cat.Element;
import com.yanqu.road.entity.activity.cat.eElementAttrTypes;
import com.yanqu.road.entity.activity.cat.eElementTypes;
import com.yanqu.road.server.gameplayer.module.activity.cat.ConnectResult;
import com.yanqu.road.server.manger.activity.cat.CatMgr;

public class OmnipotentElementAction extends BaseElementAction {

    public OmnipotentElementAction(int type) {
        super(type);
    }

    @Override
    boolean accept(Element element, Element next, ConnectResult context) {
        if(CatMgr.isBlock(next)){
            context.setRet(-1);
            return false;
        }

        //下一个是小怪（万能格子——>小怪）
        if(next.getType() == eElementTypes.Monster.getType()){
            //连接数要大于血量
            if(next.getVal() > context.getCurAttackPoint()){
                context.setRet(-1);
                return false;
            }
            //CatMgr.getLogger().info("-------- x:{},y:{}, 监牢——>小怪，扣血：{}", next.getX(), next.getY(), next.getVal());
            context.setOffsetPoint(context.getOffsetPoint() + next.getVal());
        }

        //如果有监牢
        int color = 0;
        EleAttr attr = CatMgr.hasAttr(element, eElementAttrTypes.Prison.getType());
        if(attr != null){
            color = attr.getColor();
        }

        //下一个是监牢(判断血量是否足够)
        EleAttr nextAttr = CatMgr.hasAttr(next, eElementAttrTypes.Prison.getType());
        if(nextAttr != null){
            //监牢与监牢  颜色要一致
            if(color > 0 && color != nextAttr.getColor()) {
                context.setRet(-1);
                return false;
            }
            //连接数要大于血量
            if(nextAttr.getVal() > context.getCurAttackPoint()){
                context.setRet(-1);
                return false;
            }
            //CatMgr.getLogger().info("-------- x:{},y:{}, 监牢——>小怪，扣血：{}", next.getX(), next.getY(), next.getVal());
            context.setOffsetPoint(context.getOffsetPoint() + nextAttr.getVal());
        }

        if(next.getType() == eElementTypes.Dharma.getType()){
            //达摩与监牢 颜色要一致
            if(color > 0 && color != next.getColor()){
                context.setRet(-1);
                return false;
            }
        }
        return super.acceptNext(context);
    }

    @Override
    void connect(Element element, ConnectResult context) {
        //监牢是否有血
        EleAttr attr = CatMgr.hasAttr(element, eElementAttrTypes.Prison.getType());
        if(attr != null){
            CatMgr.removeElementAttr(element, eElementAttrTypes.Prison.getType());
        }else {
            //万能格子消失
            CatMgr.clearElementTypeInfo(element);
        }
        super.connectNext(context);
    }

    @Override
    void change(Element element, ConnectResult context) {
        DharmaElementAction.onChange(element, context);
        super.changeNext(context);
    }

    @Override
    void attack(Element element, ConnectResult context) {
        super.attackNext(context);
    }

    @Override
    void generate(Element element, ConnectResult context) {

        super.generateNext(context);
    }
}
